/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view.kasse;

import control.SalgsController;
import databasehandling.HandlerFactory;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.ParseException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import model.Event;
import utility.DebugUtility;
import utility.ImageToolbox;
import utility.NumberRestrictionDocument;
import view.MainGUI;
import view.touchability.SkærmNumpad;

/**
 *
 * @author Jeppe
 */
public class TilføjEventPanel extends javax.swing.JPanel {

    private final MainGUI mainGUI;
    private final KassePanel kassePanel;

    /**
     * Creates new form TilføjEventPanels
     *
     * @param kassePanel
     */
    public TilføjEventPanel(MainGUI mainGUI, KassePanel kassePanel) {
        initComponents();
        this.mainGUI = mainGUI;
        this.kassePanel = kassePanel;
    }

    public void setScreenShotBaggrund(ImageIcon imgIcon) {
        screenShotLabel.setIcon(imgIcon);
    }

    /**
     * Metoden sætter eventlisten i tekstfeltet og markerer eventet på listens
     * første plads
     */
    public void setEventTextArea() {
        if (!jList1.isSelectionEmpty()) {
            Event event = (Event) jList1.getSelectedValue();
            visEventTxt.setText(event.toVisEventString());
        }
    }

    private void tilføjEventTilSalgOgLuk() {
        boolean succes = true;
        Event event = (Event) jList1.getSelectedValue();
        if (event == null) {
            infoLbl.setText("Intet event valgt");
            infoLbl.setForeground(Color.RED);
            succes = false;
        } else {
            infoLbl.setText("");
        }
        if (antalFremmødteTxt.getText().equals("") || !antalFremmødteTxt.getText().matches("\\d+")) {
            antalFremmødteTxt.setBorder(BorderFactory.createLineBorder(Color.RED));
            succes = false;
        }
        if(succes){
            if(Integer.parseInt(antalFremmødteTxt.getText()) > event.getAntalBestilt()){
                infoLbl.setText("Antal fremmødte er højere end bestilt.");
                antalFremmødteTxt.setBorder(BorderFactory.createLineBorder(Color.RED));
                succes = false;
            }
        }
        if (rabatTxt.getText().equals("") || !rabatTxt.getText().matches("\\d+")) {
            rabatTxt.setBorder(BorderFactory.createLineBorder(Color.RED));
            succes = false;
        }
        if (succes) {
            SalgsController.getInstance().tilføjEventTilSalg(event, Integer.parseInt(antalFremmødteTxt.getText()), Integer.parseInt(rabatTxt.getText()));
            DebugUtility.getInstance().soutDebugLine(this, 1, "Event tilføjet til salg " + SalgsController.getInstance().getAktivtSalg().getEventSalgsListe().size());
            kassePanel.opdaterKasseListeOgAktivtSalg();
            mainGUI.showPage("KassePanel");
            
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        infoLbl = new javax.swing.JLabel();
        visEventPanel = new javax.swing.JPanel();
        antalFremmødteTxt = new javax.swing.JTextField();
        antalFremmødteLbl = new javax.swing.JLabel();
        rabatLbl = new javax.swing.JLabel();
        rabatTxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        titelLbl = new javax.swing.JLabel();
        okKnap = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon) jButton1.getIcon(), 0, 0, jButton1.getWidth(), jButton1.getHeight());
            }
        };
        jScrollPane4 = new javax.swing.JScrollPane();
        visEventTxt = new javax.swing.JTextArea();
        baggrundLabel = new javax.swing.JLabel(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon) baggrundLabel.getIcon(), 0, 0, baggrundLabel.getWidth(), baggrundLabel.getHeight());
            }
        };
        baggrundOverlayKnap = new javax.swing.JButton(){
            public void paint(Graphics g){
                ImageToolbox.drawImage(g, (ImageIcon) baggrundOverlayKnap.getIcon(), 0, 0, baggrundOverlayKnap.getWidth(), baggrundOverlayKnap.getHeight());
            }
        };
        screenShotLabel = new javax.swing.JLabel();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                formComponentHidden(evt);
            }
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(null);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(null);

        jSeparator1.setBackground(new java.awt.Color(0, 0, 0));
        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 33, 430, 10);

        infoLbl.setForeground(new java.awt.Color(204, 0, 51));
        infoLbl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jPanel1.add(infoLbl);
        infoLbl.setBounds(200, 280, 230, 20);

        visEventPanel.setOpaque(false);
        visEventPanel.setLayout(null);

        antalFremmødteTxt.setText("0");
        antalFremmødteTxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                antalFremmødteTxtFocusGained(evt);
            }
        });
        antalFremmødteTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                antalFremmødteTxtKeyReleased(evt);
            }
        });
        antalFremmødteTxt.setDocument(new NumberRestrictionDocument(3));
        visEventPanel.add(antalFremmødteTxt);
        antalFremmødteTxt.setBounds(110, 10, 60, 30);

        antalFremmødteLbl.setText("Antal fremmødte:");
        visEventPanel.add(antalFremmødteLbl);
        antalFremmødteLbl.setBounds(10, 10, 100, 28);

        rabatLbl.setText("Rabat:");
        visEventPanel.add(rabatLbl);
        rabatLbl.setBounds(10, 60, 50, 20);

        rabatTxt.setText("0");
        rabatTxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                rabatTxtFocusGained(evt);
            }
        });
        rabatTxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                rabatTxtKeyReleased(evt);
            }
        });
        rabatTxt.setDocument(new NumberRestrictionDocument(3));
        visEventPanel.add(rabatTxt);
        rabatTxt.setBounds(110, 50, 60, 30);

        jLabel1.setText("%");
        visEventPanel.add(jLabel1);
        jLabel1.setBounds(180, 50, 11, 20);

        jPanel1.add(visEventPanel);
        visEventPanel.setBounds(220, 200, 200, 100);

        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(30, 60, 360, 140);

        titelLbl.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        titelLbl.setText("Events:");
        jPanel1.add(titelLbl);
        titelLbl.setBounds(30, 35, 91, 24);

        okKnap.setBackground(MainGUI.BUTTON_STANDARD_COLOR);
        okKnap.setText("OK");
        okKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okKnapActionPerformed(evt);
            }
        });
        jPanel1.add(okKnap);
        okKnap.setBounds(230, 300, 160, 56);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/lukIcon.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(400, 2, 30, 30);

        jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        visEventTxt.setEditable(false);
        visEventTxt.setColumns(20);
        visEventTxt.setRows(5);
        jScrollPane4.setViewportView(visEventTxt);

        jPanel1.add(jScrollPane4);
        jScrollPane4.setBounds(30, 210, 168, 147);

        baggrundLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/betalingsPanelOverlay.png"))); // NOI18N
        jPanel1.add(baggrundLabel);
        baggrundLabel.setBounds(0, 0, 435, 390);

        add(jPanel1);
        jPanel1.setBounds(40, 40, 435, 390);

        baggrundOverlayKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/baggrundOverlay.png"))); // NOI18N
        baggrundOverlayKnap.setText("jButton2");
        baggrundOverlayKnap.setContentAreaFilled(false);
        add(baggrundOverlayKnap);
        baggrundOverlayKnap.setBounds(0, 0, 800, 610);

        screenShotLabel.setOpaque(true);
        add(screenShotLabel);
        screenShotLabel.setBounds(0, 0, 800, 610);
    }// </editor-fold>//GEN-END:initComponents

    private void antalFremmødteTxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_antalFremmødteTxtFocusGained
        antalFremmødteTxt.setBorder(visEventTxt.getBorder());
        antalFremmødteTxt.selectAll();
    }//GEN-LAST:event_antalFremmødteTxtFocusGained

    private void rabatTxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_rabatTxtFocusGained
        rabatTxt.setBorder(visEventTxt.getBorder());
        rabatTxt.selectAll();
    }//GEN-LAST:event_rabatTxtFocusGained

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        setEventTextArea();
    }//GEN-LAST:event_jList1ValueChanged

    private void okKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okKnapActionPerformed
        tilføjEventTilSalgOgLuk();
    }//GEN-LAST:event_okKnapActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        try {
            jList1.setListData(HandlerFactory.getInstance().getEventHandler().hentAlleEvents().toArray());
            if (HandlerFactory.getInstance().getEventHandler().hentAlleEvents().toArray().length >= 1) {
                jList1.setSelectedIndex(0);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1007 \nFejl ved indlæsning af events. Prøv venligst igen senere. \n" + ex.getLocalizedMessage(), "Databasetimeout", 0);
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(this, "Fejl 1025 \nFejl ved formatering af dato. \n" + ex.getLocalizedMessage(), "Fejl", 0);
        }
        antalFremmødteTxt.setText("");
        rabatTxt.setText("");
        SkærmNumpad.getInstance().setVisible(true);
        antalFremmødteTxt.requestFocus();
        antalFremmødteTxt.selectAll();
    }//GEN-LAST:event_formComponentShown

    private void formComponentHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentHidden
        SkærmNumpad.getInstance().setVisible(false);
    }//GEN-LAST:event_formComponentHidden

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        mainGUI.showPage("KassePanel");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void antalFremmødteTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_antalFremmødteTxtKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            rabatTxt.requestFocus();
            rabatTxt.selectAll();
        }
    }//GEN-LAST:event_antalFremmødteTxtKeyReleased

    private void rabatTxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_rabatTxtKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            tilføjEventTilSalgOgLuk();
        }
    }//GEN-LAST:event_rabatTxtKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel antalFremmødteLbl;
    private javax.swing.JTextField antalFremmødteTxt;
    private javax.swing.JLabel baggrundLabel;
    private javax.swing.JButton baggrundOverlayKnap;
    private javax.swing.JLabel infoLbl;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton okKnap;
    private javax.swing.JLabel rabatLbl;
    private javax.swing.JTextField rabatTxt;
    private javax.swing.JLabel screenShotLabel;
    private javax.swing.JLabel titelLbl;
    private javax.swing.JPanel visEventPanel;
    private javax.swing.JTextArea visEventTxt;
    // End of variables declaration//GEN-END:variables
}
